// 综合测试脚本
const KeyboardController = require('./src/KeyboardController');
const ScreenshotCapture = require('./src/ScreenshotCapture');

async function runTests() {
  console.log('========================================');
  console.log('PPT远程控制器 - 综合测试');
  console.log('========================================\n');

  let passedTests = 0;
  let totalTests = 0;

  // 测试1: 检查PowerPoint是否运行
  console.log('测试1: 检查PowerPoint运行状态');
  totalTests++;
  const keyboard = new KeyboardController();
  const isRunning = await keyboard.isPowerPointRunning();
  
  if (isRunning) {
    console.log('✓ PowerPoint正在运行');
    passedTests++;
  } else {
    console.log('✗ PowerPoint未运行');
    console.log('  提示: 请先打开PowerPoint和一个演示文稿\n');
  }
  console.log('');

  // 测试2: 键盘控制功能
  if (isRunning) {
    console.log('测试2: 键盘控制功能');
    totalTests++;
    
    console.log('  尝试激活PowerPoint窗口...');
    const focused = await keyboard.focusPowerPoint();
    
    if (focused) {
      console.log('  ✓ PowerPoint窗口激活成功');
      console.log('  等待3秒后测试翻页...');
      await sleep(3000);
      
      console.log('  发送"下一页"指令...');
      const result1 = await keyboard.sendKey('right');
      
      if (result1) {
        console.log('  ✓ "下一页"指令发送成功');
        await sleep(2000);
        
        console.log('  发送"上一页"指令...');
        const result2 = await keyboard.sendKey('left');
        
        if (result2) {
          console.log('✓ 键盘控制功能正常');
          passedTests++;
        } else {
          console.log('✗ "上一页"指令发送失败');
        }
      } else {
        console.log('✗ "下一页"指令发送失败');
      }
    } else {
      console.log('✗ PowerPoint窗口激活失败');
    }
    console.log('');
  }

  // 测试3: 截图捕获功能
  console.log('测试3: 截图捕获功能');
  totalTests++;
  
  try {
    const screenshot = new ScreenshotCapture({ quality: 75, maxWidth: 800 });
    console.log('  正在捕获屏幕截图...');
    
    const startTime = Date.now();
    const imageData = await screenshot.captureSlide();
    const captureTime = Date.now() - startTime;
    
    if (imageData && imageData.length > 0) {
      console.log(`  ✓ 截图捕获成功 (${(imageData.length / 1024).toFixed(2)} KB)`);
      console.log(`  ✓ 捕获时间: ${captureTime}ms`);
      
      if (captureTime < 500) {
        console.log('✓ 截图捕获性能达标 (< 500ms)');
        passedTests++;
      } else {
        console.log(`✗ 截图捕获性能不达标 (${captureTime}ms > 500ms)`);
      }
    } else {
      console.log('✗ 截图捕获失败');
    }
  } catch (error) {
    console.log(`✗ 截图捕获出错: ${error.message}`);
  }
  console.log('');

  // 测试4: 服务器状态检查
  console.log('测试4: 服务器状态检查');
  totalTests++;
  
  try {
    const http = require('http');
    const options = {
      hostname: 'localhost',
      port: 7663,
      path: '/',
      method: 'GET',
      timeout: 3000
    };
    
    const req = http.request(options, (res) => {
      if (res.statusCode === 200) {
        console.log('✓ HTTP服务器响应正常');
        passedTests++;
      } else {
        console.log(`✗ HTTP服务器响应异常 (状态码: ${res.statusCode})`);
      }
      console.log('');
      printSummary();
    });
    
    req.on('error', (error) => {
      console.log(`✗ HTTP服务器连接失败: ${error.message}`);
      console.log('');
      printSummary();
    });
    
    req.end();
  } catch (error) {
    console.log(`✗ 服务器检查出错: ${error.message}`);
    console.log('');
    printSummary();
  }

  function printSummary() {
    console.log('========================================');
    console.log('测试总结');
    console.log('========================================');
    console.log(`通过: ${passedTests}/${totalTests}`);
    console.log(`失败: ${totalTests - passedTests}/${totalTests}`);
    
    if (passedTests === totalTests) {
      console.log('\n🎉 所有测试通过！项目可以正常使用。');
    } else if (passedTests > 0) {
      console.log('\n⚠️  部分测试失败，请检查上述错误信息。');
    } else {
      console.log('\n❌ 所有测试失败，请检查配置和环境。');
    }
    console.log('========================================');
    
    process.exit(passedTests === totalTests ? 0 : 1);
  }
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

runTests().catch(error => {
  console.error('测试过程中发生错误:', error);
  process.exit(1);
});
